define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consult/order/index' + location.search,
                    add_url: 'consult/order/add',
                    edit_url: 'consult/order/edit',
                    // del_url: 'consult/order/del',
                    multi_url: 'consult/order/multi',
                    import_url: 'consult/order/import',
                    table: 'consult_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns:Controller.api.list_columns(table),
            });
            // 为表格绑定事件
            Table.api.bindevent(table);


            var table1 = $("#table1");
            // 初始化表格
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar1',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams:function(params){
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.pay_type = 'code';
                    params.op.pay_type = '<>';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns:Controller.api.list_columns(table1),
            });
            // 为表格绑定事件
            Table.api.bindevent(table1);

            // 兑换码订单
            var table2 = $("#table2");
            // 初始化表格
            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar2',
                fixedColumns: true,
                // fixedRightNumber: 1,
                queryParams:function(params){
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.pay_type = 'code';
                    params.op.pay_type = '=';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns:Controller.api.list_columns(table2),
            });
            // 为表格绑定事件
            Table.api.bindevent(table2);
            Controller.api.formbindevent(table2);



            // 退款订单
            var table3 = $("#table3");
            // 初始化表格
            table3.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar3',
                fixedColumns: true,
                // fixedRightNumber: 1,
                queryParams:function(params){
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.is_refund = 1;
                    params.op.is_refund = '=';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns:Controller.api.list_columns(table3),
            });
            // 为表格绑定事件
            Table.api.bindevent(table3);
            Controller.api.formbindevent(table3);

        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'consult/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'consult/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'consult/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        exchange: function () {
            Controller.api.bindevent();
        },
        refund: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formbindevent:function(table){

                var parenttable = table.closest('.bootstrap-table');
                var options = table.bootstrapTable('getOptions');
                var toolbar = $(options.toolbar, parenttable);

                // 修改兑换码价格操作
                toolbar.on("click", ".btn-handler", function () {
                    //如果操作全部则ids可以置为空
                    var ids = Table.api.selectedids(table);
                    Fast.api.open('consult/order/exchange/ids/'+ids, __("修改兑换码价格操作"), {area: ["60%", "80%"], maxmin: true, moveOut: false});
                });

                // 审核查看，完成退款
                toolbar.on("click", ".btn-checked", function () {
                    //如果操作全部则ids可以置为空
                    var ids = Table.api.selectedids(table);
                    Fast.api.open('consult/order/refund/ids/'+ids, __("审核退款申请操作"), {area: ["60%", "80%"], maxmin: true, moveOut: false});
                });

            },
            list_columns: function(tables){
                console.log(tables.selector)
                var field_columns =[
                    {checkbox: true},
                    {field: 'id', title: __('Id'),operate: 'LIKE'},
                    {field: 'user.nickname', title: __('User_id'),operate: false},
                    {field: 'xlzxinfo.full_name', title: __('Xlzxinfo_id'),operate: false},
                    {field: 'type', title: __('咨询类型'), searchList: {"1":__('立即咨询'),"2":__('预约咨询')}, formatter: Table.api.formatter.status},
                    {field: 'price', title: __('Price'), operate:'BETWEEN',operate: false},
                    {field: 'exchange_price', title: __('兑换码金额'),operate:false,
                        visible: tables.selector =='#table2' ? true:false,
                        operate:'BETWEEN'
                    },

                    {field: 'consult_type', title: __('沟通类型'), searchList: {"1":__('文字'),"2":__('语音')}, formatter: Table.api.formatter.status},
                    {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                    {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,operate: false},
                    // {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                    // {field: 'commission_price', title: __('Commission_price'), operate:'BETWEEN'},
                    // {field: 'commission_rate', title: __('Commission_rate'), operate:'BETWEEN'},
                    {field: 'pay_status', title: __('支付状态'), searchList: {"created":__('待支付'),"paid":__('已支付'),"expired":__('失效')}, formatter: Table.api.formatter.status},
                    {field: 'status', title: __('订单状态'), searchList: {"1":__('待处理'),"2":__('处理中'),"3":__('已完成'),"4":__('已过期'),"5":__('已关闭')}, formatter: Table.api.formatter.status},
                    // {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                    // {field: 'wechat', title: __('Wechat'), operate: 'LIKE'},
                    // {field: 'qq', title: __('Qq'), operate: 'LIKE'},
                    {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    {field: 'operate', title: __('Operate'), table: tables, events: Table.api.events.operate, buttons: [
                            {
                                name: 'ajax',
                                text: '',
                                title: '关闭',
                                classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                icon: 'fa fa-close',
                                url: 'consult/order/closed',
                                confirm: '确定要关闭吗？',
                                visible: function (rr) {
                                    if(rr.pay_status == 'paid' && rr.is_refund == 0){
                                        return true;
                                    }
                                    return false;
                                },
                                success: function (data, ret) {
                                    $(".btn-refresh").trigger("click");
                                    // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                    //如果需要阻止成功提示，则必须使用return false;
                                    //return false;
                                },
                                error: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            }
                        ],
                        // formatter: Table.api.formatter.buttons
                        formatter: Table.api.formatter.operate
                    }
                ];
                return [field_columns];
            }
        }
    };
    return Controller;
});
